package com.hj.file.service.impl;

import com.hj.file.domain.LuceneFileDO;
import com.hj.file.service.LuceneFileService;
import com.hj.file.util.LuceneUtils;
import com.hj.file.util.POIUtils;
import com.itextpdf.text.DocumentException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class LuceneFileServiceImpl implements LuceneFileService {
    @Value("${lucene.filePath}")
    private String luceneFilePath;
    @Value("${lucene.indexDir}")
    private String indexDirectory;

    @Override
    public int uploadFiles(MultipartFile[] files, String fileDir) {
        List<LuceneFileDO> fileDOS = new ArrayList<>();
        for (MultipartFile file : files) {
            try {
                LuceneFileDO fileDO = new LuceneFileDO();
                String fileName = file.getOriginalFilename();
                fileDO.setFileName(fileName);
                fileDO.setUrl("originFile");
                String type = fileName.substring(fileName.lastIndexOf(".") + 1);
                fileDO.setType(type);
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                String pdfPath = "originFile" + File.separator + "pdf" + File.separator;
                POIUtils.mkDirs(luceneFilePath + File.separator + pdfPath);
                fileDO.setPdfPath(pdfPath + uuid + ".pdf");
                InputStream inputStream = file.getInputStream();
                String outPath = luceneFilePath + File.separator + "originFile";
                POIUtils.mkDirs(outPath);
                OutputStream outputStream = new FileOutputStream(new File(outPath + File.separator + fileName));
                fileDOS.add(fileDO);
                IOUtils.copy(inputStream, outputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        new Thread(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (LuceneFileDO fileDO : fileDOS) {
                String path = luceneFilePath + File.separator + fileDO.getUrl();
                String pdfPath = fileDO.getPdfPath();
                try {
                    if ("pdf".equals(fileDO.getType())) {
                        LuceneUtils.pdf2lucene(path, fileDO.getFileName(), indexDirectory);
                        InputStream inputStream = new FileInputStream(new File(path + File.separator + fileDO.getFileName()));
                        OutputStream outputStream = new FileOutputStream(new File(luceneFilePath + File.separator + pdfPath));
                        IOUtils.copy(inputStream, outputStream);
                    } else if ("doc".equals(fileDO.getType())) {
                        LuceneUtils.doc2lucene(path, fileDO.getFileName(), indexDirectory);
                        fileDO.setPdfPath(pdfPath);
                        POIUtils.doc2pdf(path, fileDO.getFileName(), luceneFilePath + File.separator + pdfPath);
                    } else if ("docx".equals(fileDO.getType())) {
                        LuceneUtils.docx2lucene(path, fileDO.getFileName(), indexDirectory);
                        fileDO.setPdfPath(pdfPath);
                        POIUtils.docx2pdf(path, fileDO.getFileName(), luceneFilePath + File.separator + pdfPath);
                    }
                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (TransformerException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (DocumentException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        return fileDOS.size();
    }
}
